﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Imaginecup.factory;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using imaginecup.objectManager;
using imaginecup.information;
using imaginecup.util;
using Imaginecup.util;
using Imaginecup.stage;
using Imaginecup.gameState.malaria.stage;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.vertex2D;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Imaginecup.gameState.malaria.component.entity.panel;
using Imaginecup.gameState.malaria.component.factory;

namespace Imaginecup.gameState.malaria
{
    public class RainState : GameState
    {
        private IStageMan m_StageMan;
        private ContentManager m_Content;
        private Vector2 m_MainScreenSize;
        private static RainState m_RainState = null;



        private RainState()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

            m_MainScreenSize = new Vector2(800, 400);
            //제일 처음 생성되는 시점에서 initState를 호출한다.( 호출하지 않으면 ObjectManager


            Vertex2DPipeMan pipeMan = Vertex2DPipeMan.GetRenderPipeMan();

            pipeMan.SetScreenSize(new Vector2(800, 480));
            pipeMan.InitPipeMan2D();

        }

        public static RainState GetRainState()
        {
            if (m_RainState == null)
            {
                m_RainState = new RainState();
            }
            return m_RainState;
        }

        public void SetStageMan(IStageMan stageMan)
        {
            m_StageMan = stageMan;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            //    m_TrailLayer = new TouchTrailLayer();
            //     ObjectManager objMan = ObjectManager.GetObjectManager();
            //     objMan.Init(new MalariaCompInit());
            //     m_StageMan.Init();
        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)  공통사항                                */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            
            m_StageMan.CompleteResource();

        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            //      ObjectManager.GetObjectManager().Release();

#if WINDOWS
            m_Game.Components.Remove(KeyboardInputHandler.GetInputHandler());
#endif
            m_StageMan.Release();
            ResourceMan.GetResourceMan().Release();
            SoundMan.GetSoundMan().ReleaseSound();
            m_Content.Unload();
        }

        public override void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() != this)
            {
                this.Enabled = false;
            }
            else
                this.Enabled = true;

            base.AddStateEvent();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {


            IStageLoop stg = m_StageMan.GetStage();
            stg.Update(gameTime);

            //            if ( m_StageMan.IsGameOver() )
            //            {

            ////                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Malaria));
            //            }

            if (m_StageMan.IsClear())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Select));
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            IStageLoop stg = m_StageMan.GetStage();
            stg.Draw(gameTime, m_GraphicData.GetSpriteBatch());

            base.Draw(gameTime);

#if XBOX
            //prevMouse = MouseState;
            //MouseState = Mouse.GetState();

            //if (MouseState.X > prevMouse.X)
            //{
            //    temp.X += 3;
            //}
            //else if( MouseState.X < prevMouse.X)
            //{
            //    temp.X -= 3;
            //}
            //m_GraphicData.GetSpriteBatch().Begin();
            //m_GraphicData.GetSpriteBatch().Draw(m_Cursor,  temp , Color.White);
            //m_GraphicData.GetSpriteBatch().End();

#endif
        }


        public Vector2 GetMainScreenSize()
        {
            return m_MainScreenSize;
        }
    }
}
